import uploadApi from "@/config/config.js"
import {
	useStore
} from '@/store/index.js'

//图片压缩
export function Upload(file) {
// #ifdef H5
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${uploadApi.baseURL}/file/upload`,
			name: 'multipartFile',
			filePath: file,
			header: {
				token: useStore().token || 'f2650c644ea03c0f4cfb8025fe9436a5'
			},
			success: (res) => {
				try {
					res.data = JSON.parse(res.data)
					if (res.data.code == 200) {
						resolve(res.data.data)
					} else {
						reject(res.data.data);
						wx.showToast({
							title: '图片上传失败',
							icon: 'none'
						})
					}
				} catch (err) {
					reject(err);
					wx.showToast({
						title: '图片上传失败',
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				reject(err);
				wx.showToast({
					title: '图片上传失败',
					icon: 'none'
				})
			}
		});
	})
	
// #endif
	return new Promise((resolve, reject) => {
		console.log(file)
		uni.compressImage({
			src: file,
			quality: 40,
			success: res => {
				uni.uploadFile({
					url: `${uploadApi.baseURL}/file/upload`,
					name: 'multipartFile',
					filePath: res.tempFilePath,
					header: {
						token: useStore().token || '283d5c6bed3c7ce2b95d8d3f00e61066'
					},
					success: (res) => {
						try {
							res.data = JSON.parse(res.data)
							if (res.data.code == 200) {
								resolve(res.data.data)
							} else {
								reject(res.data.data);
								wx.showToast({
									title: '图片上传失败',
									icon: 'none'
								})
							}
						} catch (err) {
							reject(err);
							wx.showToast({
								title: '图片上传失败',
								icon: 'none'
							})
						}
					},
					fail: (err) => {
						reject(err);
						wx.showToast({
							title: '图片上传失败',
							icon: 'none'
						})

					}
				});
			}
		})
	})


}